package cn.xiehangs;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.List;
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Array;

public class 聊天框显示部分  {
    Texture background;
    LazyBitmapFont font;
    List<String> list2;
    ScrollPane scrollPane;
    int x ,y;
    int stageWidth,stageHeight;
    public boolean focus = false;
    int fontHeight = 20;
    int i=0;
    int sum=0;
    public 聊天框显示部分(int x,int y,int stageWidth,int stageHeight) {
        this.x = x;
        this.y = y;
        this.stageWidth = stageWidth;
        this.stageHeight = stageHeight;
        background = new Texture("chat.png");
        FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.classpath("font.ttf"));
        Color color = new Color(0, 0, 0, 1);
        font = new LazyBitmapFont(generator, fontHeight, color);
        List.ListStyle liststyle= new List.ListStyle();
        liststyle.font = font;
        liststyle.selection = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("chat.png"))));
        list2 = new List<String>(liststyle);
        String[] items = {};
        list2.setItems(items);
        list2.pack();
        ScrollPane.ScrollPaneStyle scrollPaneStyle = new ScrollPane.ScrollPaneStyle();
        scrollPane = new ScrollPane(list2, scrollPaneStyle);
        scrollPane.setHeight(stageHeight);
        scrollPane.setWidth(stageWidth);
        scrollPane.setPosition(x, y);
    }
    public void add(String str){
        Array array = list2.getItems();
        array.add("系统消息"+i+":"+str);
        list2.setItems(array);
        i++;
        sum++;
        //将滚动条滚动到最底部
        if(!focus) {
            scrollPane.setScrollY(sum * (fontHeight+20));
        }
    }
    public void chat(String name,String str){
        Array array = list2.getItems();
        array.add(name+":"+str);
        list2.setItems(array);
        scrollPane.setScrollY(sum * (fontHeight+20));
        sum++;
    }
}
